001    /*
002     * Copyright 2005 Stephen McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.transit.tools;
020    
021    import java.io.File;
022    import java.net.URI;
023    import java.net.URL;
024    
025    import net.dpml.transit.artifact.Handler;
026    
027    import org.apache.tools.ant.BuildException;
028    import org.apache.tools.ant.Project;
029    
030    /**
031     * The get task handles the retrival of a rresource and the binding of the resource filename
032     * to a project property.
033     *
034     * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a>
035     * @version 1.0.0
036     */
037    public class GetTask extends TransitTask
038    {
039       /**
040        * The uri of the artifact to load.
041        */
042        private String m_uri;
043    
044       /**
045        * The name of a property under which the locally cached artifact filename will be bound.
046        */
047        private String m_name;
048    
049       /**
050        * Set the project.
051        * @param project the current project
052        */
053        public void setProject( Project project )
054        {
055            setTaskName( "get" );
056            super.setProject( project );
057        }
058    
059       /**
060        * Set the artifact uri of the plugin from which the task is to be loaded.
061        * @param uri an artifact plugin uri
062        */
063        public void setUri( String uri )
064        {
065            m_uri = uri;
066        }
067    
068       /**
069        * Set the name of a property into which the local file path will be assigned.
070        * @param name the ant property name
071        */
072        public void setProperty( String name )
073        {
074            m_name = name;
075        }
076    
077       /**
078        * Return the artifact uri of the plugin.
079        * @return the artifact uri
080        */
081        private URI getURI()
082        {
083            try
084            {
085                return new URI( m_uri.toString() );
086            }
087            catch( Throwable e )
088            {
089                final String error =
090                  "Cound not convert the supplied uri spec ["
091                  + m_uri
092                  + "] to a formal URI.";
093                throw new BuildException( error, e, getLocation() );
094            }
095        }
096    
097       /**
098        * Return the ant property name.
099        * @return the property name
100        */
101        private String getPropertyName()
102        {
103            if( null == m_name )
104            {
105                final String error =
106                  "The required 'property' attribute is not declared.";
107                throw new BuildException( error, getLocation() );
108            }
109            else
110            {
111                return m_name;
112            }
113        }
114    
115       /**
116        * Load the resource and assign the locally cached file path to the supplied property name.
117        * @exception BuildException if an error occurs during resource resolution
118        */
119        public void execute() throws BuildException
120        {
121            if( null == m_uri )
122            {
123                final String error =
124                  "Missing uri attribute.";
125                throw new BuildException( error, getLocation() );
126            }
127    
128            String name = getPropertyName();
129    
130            try
131            {
132                String spec = getURI().toString();
133                log( "artifact: " + spec );
134                URL url = new URL( (URL) null, spec, new Handler() );
135                File file = (File) url.getContent( new Class[]{File.class} );
136                getProject().setNewProperty( name, file.getAbsolutePath() );
137            }
138            catch( Throwable e )
139            {
140                final String error =
141                  "Unexpected error while attempting to resolve artifact uri ["
142                  + m_uri
143                  + "]";
144               throw new BuildException( error, e, getLocation() );
145            }
146        }
147    }